package com.example.ftp.Controller.SiteManage;

import com.example.ftp.Beans.Site;
import com.example.ftp.Service.Impl.SiteManagerServiceImpl;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Optional;

/**
 * @author yhp
 */

public class SiteList {
    @FXML
    Button newBtn;

    @FXML
    TableView<Site> siteTable;

    private static boolean state;

//    /*站点列表*/
//    private List<Site> list = new ArrayList<>();

//    private ObservableList<Site> list;

    private SiteManagerServiceImpl siteList = new SiteManagerServiceImpl();

    private static com.example.ftp.Beans.Site newSite;

    private String[] siteListHeader = new String[]{"name", "ip", "port", "userName", "password", "note"};

    private static Site selectedSite;

    private static SiteList controllerInstance;



    public void initialize(){
        // 加载站点列表
        siteTable.refresh();

        siteList.Deserialization();

//        list = siteList.getSiteList();
//        data = FXCollections.observableArrayList();
//        data.addAll(list);
//
        siteTable.setItems(siteList.getSiteList());

        // 设置站点列表数据
        for (int i = 0; i < siteTable.getColumns().size(); i ++) {
            siteTable.getColumns().get(i).setCellValueFactory(new PropertyValueFactory<>(siteListHeader[i]));
        }

        // 设置行
        siteTable.setRowFactory(t -> {
            // 上下文菜单
            TableRow<Site> row = new TableRow<>();
            SiteItemContextMenu contextMenu = new SiteItemContextMenu();
            row.setContextMenu(contextMenu);

            // 监听点击事件
            row.setOnMouseClicked(e -> {
                SiteList.setSelectedSite(row.getItem());
            });

            return row;
        });
    }

    /**
     * 增加站点
     */
    public void addSite() throws IOException {
        // 获取新增站点
        SiteInfo siteInfo = new SiteInfo();
        Site s = siteInfo.open(null);
        if( s != null){
            siteList.AddSite(s);
        }
    }

    /**
     * 删除站点
     */
    public void delSite() throws IOException {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Delete Site");
        alert.setHeaderText("Confirm");
        alert.setContentText("Are you sure you want to delete it? ");

        Optional<ButtonType> result = alert.showAndWait();
        result.ifPresent(s -> {
            if (result.get() == ButtonType.OK){
                siteList.DeleteSite(getSelectedSite().getIp(), String.valueOf(getSelectedSite().getPort()));
                refreshList();
            }
        });
    }

    public static void setNewSite(Site newSite,boolean state) {
        SiteList.newSite = newSite;
        SiteList.state = state;
    }

    public static Site getSelectedSite() {
        return selectedSite;
    }

    public static void setSelectedSite(Site selectedSite) {
        SiteList.selectedSite = selectedSite;
    }

    public static void showSiteList() throws IOException {
        Stage stage = new Stage();

        SiteList sl = new SiteList();
        FXMLLoader fxmlLoader = new FXMLLoader(sl.getClass().getResource("site-list.fxml"));

        Scene scene = new Scene(fxmlLoader.load(), 600, 400);
        setControllerInstance(fxmlLoader.getController());
        stage.setTitle("Sites Manager");
        stage.setScene(scene);
        stage.show();
    }


    public static SiteList getControllerInstance() {
        return controllerInstance;
    }

    public static void setControllerInstance(SiteList controllerInstance) {
        SiteList.controllerInstance = controllerInstance;
    }

    public void refreshList() {
        siteList.Serialization();
        siteList.Deserialization();
        siteTable.refresh();
    }

    public Button getNewBtn() {
        return newBtn;
    }

    public void setNewBtn(Button newBtn) {
        this.newBtn = newBtn;
    }

    public TableView<Site> getSiteTable() {
        return siteTable;
    }

    public void setSiteTable(TableView<Site> siteTable) {
        this.siteTable = siteTable;
    }

    public static boolean isState() {
        return state;
    }

    public static void setState(boolean state) {
        SiteList.state = state;
    }

    public SiteManagerServiceImpl getSiteList() {
        return siteList;
    }

    public void setSiteList(SiteManagerServiceImpl siteList) {
        this.siteList = siteList;
    }

    public static Site getNewSite() {
        return newSite;
    }

    public static void setNewSite(Site newSite) {
        SiteList.newSite = newSite;
    }

    public String[] getSiteListHeader() {
        return siteListHeader;
    }

    public void setSiteListHeader(String[] siteListHeader) {
        this.siteListHeader = siteListHeader;
    }
}
